package io.renren.modules.secretary.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.common.entity.Teacher;
import io.renren.modules.secretary.service.StudentService;
import io.renren.modules.secretary.service.TeacherService;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.renren.common.utils.ShiroUtils.getUserId;

/**
 * @author wks
 * @date 2022/2/7 16:21
 */
@RestController
@RequestMapping("/secretary/teacher")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 所有学生列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        Page<Map<String,Object>> page = new Page<>(Long.valueOf(String.valueOf(params.get("limit"))) * (Long.valueOf(String.valueOf(params.get("page")))-1) + 1,Long.valueOf(String.valueOf(params.get("limit"))) * Long.valueOf(String.valueOf(params.get("page"))));
        IPage<Map<String, Object>> mapIPage = teacherService.selectStudentList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);
    }

    /**
     * 教师角色列表
     */
    @GetMapping("/select")
    public R select(){
        Map<String, Object> map = new HashMap<>();
        map.put("role_id",4);
        List<SysRoleEntity> list = (List<SysRoleEntity>) sysRoleService.listByMap(map);

        return R.ok().put("list", list);
    }

    /**
     * 所有非班主任教师集合
     * @return
     */
    @GetMapping("/selectTeachers")
    public R selectTeachers(){
        List<Teacher> teachers = teacherService.selectTeachers();

        return R.ok().put("teachers", teachers);
    }

    @SysLog("保存班主任")
    @PostMapping("/save")
    public R save(@RequestBody SysUserRoleEntity userRole){
        sysUserRoleService.save(userRole);
        return R.ok();
    }

    /**
     * 保存用户
     */
    @SysLog("书记添加教师")
    @PostMapping("/saveTeacher")
    public R save(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);
        user.setCreateUserId(ShiroUtils.getUserId());
        sysUserService.masterSaveTeacher(user);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("书记修改教师")
    @PostMapping("/updateTeacher")
    //@RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        user.setCreateUserId(ShiroUtils.getUserId());
        sysUserService.masterUpdateStudent(user);

        return R.ok();
    }

}
